package com.zjh.Guide.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;
import com.zjh.Guide.model.Notification;
import com.zjh.Guide.model.page.NotificationPage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.Date;
import java.util.List;

/**
 * Created by zZz on 2017/5/7 0007.
 */
public class GuideController extends Controller {

    /**
     * 根据子栏目id获取新闻内容
     *
     * @param columnId|id|Integer|必填
     * @param beginTime|开始时间|String|选填
     * @param endTime|结束时间|String|选填
     * @param title|标题|String|选填
     * @param pageSize|每页条数|Integer|是
     * @param pageNumber|当前页|Integer|是
     * @title 根据子栏目id获取公告内容
     */
    @RequiresPermissions("Guide:read")
    public void getNewsByColumnId()
    {
        NotificationPage newsPage = getBean(NotificationPage.class, "");
        Page<Notification> page = Notification.dao.paginate(newsPage);

        renderJson(ResultKit.success(page));
    }

    /**
     * 根据新闻内容
     *
     * @param id|id|Integer|必填
     * @title 根据新闻内容
     */
    @RequiresPermissions("Guide:read")
    public void getNewsById()
    {
        renderJson(ResultKit.success(Notification.dao.findById(getParaToInt("id"))));
    }

    /**
     * 新增新闻
     *
     * @param Columnid|子栏目id|Integer|必填
     * @param content|内容|String|选填
     * @param title|标题|String|选填
     * @title 新增新闻
     */
    @RequiresPermissions("Guide:update")
    public void addNews()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        boolean result = new Notification()
                .setTitle(getPara("title"))
                .setContent(getPara("content"))
                .setCreateTime(new Date())
                .setCreateUser(user.getId())
                .setColumnid(getParaToInt("Columnid"))
                .save();
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("add_success", Constant.NOTIFICATION_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("add_fail", Constant.NOTIFICATION_MODULE, this)));
        }
    }
    /**
     * 根据新闻id删除新闻
     *
     * @param id|id|Integer|必填
     * @title 根据新闻id删除新闻
     */
    @RequiresPermissions("Guide:update")
    public void deleteNewsById()
    {
        boolean result = Notification.dao.deleteById(getParaToInt("id"));
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("remove_success", Constant.NOTIFICATION_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("remove_fail", Constant.NOTIFICATION_MODULE, this)));
        }
    }

    /**
     * 根据新闻id更新新闻
     *
     * @param id|id|Integer|必填
     * @param content|内容|String|选填
     * @param title|标题|String|选填
     * @title 根据新闻id更新新闻
     */
    @RequiresPermissions("Guide:update")
    public void updateNewsById()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        boolean result = Notification.dao.findById(getParaToInt("id"))
                .setTitle(getPara("title"))
                .setContent(getPara("content"))
                .setEditTime(new Date())
                .setEditUser(user.getId())
                .update();
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("edit_success", Constant.NOTIFICATION_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("edit_fail", Constant.NOTIFICATION_MODULE, this)));
        }
    }

}
